<?php
namespace Home\Controller;
use Think\Controller;

/**
* 参数  控制器
*/
class FunctionController extends HomeBasicController{
	
	public $Project = '';
	public $Module = '';
	public $Function ='';
	public $Parameter = '';

	public function _initialize(){
		parent::_initialize();
		$this->checkLogin();
		$this->Project = D('Project');
		$this->Module = D('Module');
		$this->Function = D('Function');
		$this->Parameter = D('Parameter');
		$active = $_GET['f_id'];
		$this->assign('active',$active);
	}

	/**
	 * 显示参数信息
	 */
	public function index(){

		//查询当前方法的信息
		$where['f_id'] = $_GET['f_id'];
		$function_info = $this->Function->findFunction($where);
		//查询当前方法下的参数
		$f_parameter = unserialize($function_info['f_pa']); 

		$where1['p_id'] = $_GET['p_id'];
    	$project_info = $this->Project->findProject($where1);
    	//查询所有模块名称
    	$module_list = $this->Module->selectModule($where1,'','');

    	//查询当前模块名称
    	$where2['mo_id'] = $_GET['mo_id'];
    	$module_info = $this->Module->findModule($where2);

    	//查询当前模块下的方法名称
    	foreach ($module_list as $k => $v){
    		$where2['mo_id']  = $v['mo_id'];
    		$module_list[$k]['function_list'] = $this->Function->selectFunction($where2,'','');
    	}
    	
    	$this->assign('project_info',$project_info);
    	$this->assign('module_list',$module_list);
    	$this->assign('module_info',$module_info);
		$this->assign('function_info',$function_info);
		$this->assign('f_parameter',$f_parameter);
		$this->display();
	}


	/**
	 * 修改方法
	 */
	public function editFunction(){
        if(session('M_TYPE') == 2){
            //客户想删除方法
            $mo_id = $this->Function->where(array('f_id'=>$_POST['fid']))->getField('mo_id');
            $p_id = $this->Module->where(array('mo_id'=>$mo_id))->getField('p_id');
            if($p_id == 22){
                $data['info'] = '您没有编辑权限';
                $this->ajaxReturn($data);
            }
        }
		$where['f_id'] = $_POST['fid'];
		$fnames = $_POST['fnames'];
		$data[''.$fnames.''] = $_POST['vals'];
		$res = $this->Function->editFunction($where,$data);
		if($res){
			$this->success('修改成功');
		}else{
			$this->success('修改失败');
		}
	}
	
	/**
	 * 修改所选方法下的参数
	 */
	public function editPa(){
        if(session('M_TYPE') == 2){
            //客户想修改方法参数
            $mo_id = $this->Function->where(array('f_id'=>$_POST['f_id']))->getField('mo_id');
            $p_id = $this->Module->where(array('mo_id'=>$mo_id))->getField('p_id');
            if($p_id == 22){
                $this->error('您没有修改权限');
            }
        }
		$where['f_id'] = $_POST['f_id'];
		$arr[] = array();
		foreach ($_POST['p_name'] as $k => $v) {
			$arr[$k]['p_type'] = $_POST['p_type'][$k];
			$arr[$k]['p_name'] = $_POST['p_name'][$k];
			$arr[$k]['p_desc'] = $_POST['p_desc'][$k];
			$arr[$k]['p_is_null'] = $_POST['p_is_null'][$k];
            $arr[$k]['default_val'] = $_POST['default_val'][$k];
		}
		$arr_str = serialize($arr);
		$data['f_pa'] =$arr_str;
		$res = $this->Function->editFunction($where,$data);
		if($res){
			$this->success('修改成功');
		}else{
			$this->error('修改失败');
		}
	}

}

?>